/*
 * Copyright 2011 Subhabrata Ghosh <subho.ghosh at langene.net>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



package net.langene.nlp.training.en;

//~--- non-JDK imports --------------------------------------------------------

import net.langene.common.Job;
import net.langene.nlp.corpus.anc.OpenANCHandler;
import net.langene.nlp.model.Sentence;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//~--- JDK imports ------------------------------------------------------------

import java.util.List;

//~--- classes ----------------------------------------------------------------

/**
 *
 * @author Subhabrata Ghosh <subho.ghosh at langene.net>
 */
public class OpenANCTrainingJob extends Job<Object> {
    private static Logger	_logger = LoggerFactory.getLogger(OpenANCTrainingJob.class);

    //~--- fields -------------------------------------------------------------

    private OpenANCTrainer	trainer = null;

    /** Field description */
    protected EnumWorkflowState	workflowState = EnumWorkflowState.Pending;
    private OpenANCHandler	handler       = new OpenANCHandler();
    private String		sourcefile;

    //~--- constant enums -----------------------------------------------------

    /**
     * Enum defines the workflow states of a OpenANC Training Job can be in.
     *
     */
    public static enum EnumWorkflowState {

        /**
         * Job has been scheduled but not started.
         */
        Pending,

        /**
         * Corpus data loaded.
         */
        Loaded,

        /**
         * Training finished.
         */
        Processed
    }

    //~--- constructors -------------------------------------------------------

    /**
     * Create a new instance of the Open ANC Training Job.
     *
     * @param trainer Handle to the trainer.
     *
     * @throws Exception
     */
    public OpenANCTrainingJob(OpenANCTrainer trainer) throws Exception {
        this.trainer = trainer;
        handler.setup();
    }

    //~--- get methods --------------------------------------------------------

    /**
     * Get the content source this Job is operating on.
     *
     * @return
     */
    public String getSource() {
        return sourcefile;
    }

    //~--- methods ------------------------------------------------------------

    @Override
    protected Object execute() throws Exception {
        _logger.debug("Running Status : " + status.name());
        sourcefile = trainer.getSource();

        if (sourcefile == null) {
            return false;
        }

        handler.load(sourcefile, true);

        List<Sentence>	sentences = handler.getSentences();

        if ((sentences == null) || (sentences.size() <= 0)) {
            return false;
        }

        workflowState = EnumWorkflowState.Loaded;
        _logger.debug("Loaded sentences for source [" + sourcefile + "] (" + getRunningTime()
                      + "ms)(" + status.name() + ")");
        workflowState = EnumWorkflowState.Processed;

        return true;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
